package com.gmrz.uap.fidolog;

import com.gmrz.uap.cfg.SystemConst;
import com.gmrz.uap.common.AuthTypeInterceptor;
import com.gmrz.uap.common.ConditionConst;
import com.gmrz.uap.common.ControllerSupport;
import com.gmrz.uap.common.OperTypeInterceptor;
import com.gmrz.uap.model.FidoLog;
import com.gmrz.uap.resource.ResourceInterceptor;
import com.gmrz.uap.util.DateUtil;
import com.gmrz.uap.util.Strings;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.joda.time.DateTime;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FidoLogController extends ControllerSupport {

    private static final Logger LOG = LogManager.getLogger(FidoLogController.class);

    /**
     * 白名单管理主页面
     */
    @Before({ResourceInterceptor.class, OperTypeInterceptor.class, AuthTypeInterceptor.class})
    public void index() {
    }

    /**
     * 根据前台发过来的参数进行查询，可以进行分页，排序
     */
    public void list() {

        int page = getParaToInt(SystemConst.DATATABLE_PAGINATE_OFFSET, SystemConst.DATATABLE_PAGINATE_OFFSET_DEFAULT);
        int rows = getParaToInt(SystemConst.DATATABLE_PAGINATE_LIMIT, SystemConst.DATATABLE_PAGINATE_LIMIT_DEFAULT);

        String sort = getPara(SystemConst.DATATABLE_PAGINATE_SORT, SystemConst.DEFAULT_ORDERBY_CREATETS);
        String order = getPara(SystemConst.DATATABLE_PAGINATE_ORDER, SystemConst.DATATABLE_PAGINATE_ORDER_DEFAULT);

        String startTime = getPara(ConditionConst.CONDITION_START_TIME, null);
        String endTime = getPara(ConditionConst.CONDITION_END_TIME, null);

        FidoLog fidoLog = getModel(FidoLog.class);
//        if (StringUtils.isBlank(startTime) && StringUtils.isBlank(endTime)){
//            //30天前的日期
//            DateTime retainDateTime = new DateTime().minusDays(30);
//            startTime = retainDateTime.toString("yyyy-MM-dd");
//            endTime = new DateTime().toString("yyyy-MM-dd");
//        }
        fidoLog.put(ConditionConst.CONDITION_START_TIME, startTime);//开始时间
        fidoLog.put(ConditionConst.CONDITION_END_TIME, endTime);//结束时间

        String tenantPara = getCookie("tenantId");
        String tenantId = getPara("tenantId", tenantPara);
        fidoLog.setTenantId(tenantId);

        LOG.debug("FIDO Log request params : {}", fidoLog);

        long start = System.currentTimeMillis();

        Page<FidoLog> descPage = FidoLogService.me.findFidoLogListForPaginate(fidoLog, page, rows, sort, order, condition);

        LOG.debug(descPage.getTotalRow()+" select log all time:" + (System.currentTimeMillis() - start));

        result.put(SystemConst.DATATABLE_PAGINATE_TOTAL, descPage.getTotalRow());
        result.put(SystemConst.DATATABLE_PAGINATE_ROWS, descPage.getList());

        result.put("startTime",startTime);
        result.put("endTime",endTime);
        render(responseJsonRender(result));
    }

    public void info() {
        String flID = getPara("flID");
        String infoType = getPara("infoType");
        String result = null;
        try {
            result = FidoLogService.me.findFidoLogInfo(flID,infoType);
        } catch (UnsupportedEncodingException e) {
            LOG.error("get info error",e);
        }
        render(responseJsonRender(result));
    }
}
